<template>
  <el-dialog :visible.sync="visible" :title="!dataForm.id ? $t('add') : $t('update')" :close-on-click-modal="false"
             :close-on-press-escape="false">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmitHandle()"
             :label-width="$i18n.locale === 'en-US' ? '120px' : '80px'">
<!--      <el-form-item label="猪舍id" prop="houseId">-->
<!--        <el-input v-model="dataForm.houseId" placeholder="猪舍id"></el-input>-->
<!--      </el-form-item>-->
      <el-row :gutter="15">
        <el-col :span="12">
          <el-form-item size='small' label="猪舍" prop="houseId">
            <el-select v-model="dataForm.houseId" @change="house_change" placeholder="猪舍">
              <el-option
                  v-for="(house, house_id) in houses"
                  :key="house_id"
                  :label="house.name"
                  :value="house_id">
              </el-option>
            </el-select>
          </el-form-item></el-col>
        <el-col :span="12">
          <el-form-item label="死淘" prop="category">
            <el-select v-model="dataForm.category" placeholder="类别">
              <el-option label="死亡" value="死亡"></el-option>
              <el-option label="淘汰" value="淘汰"></el-option>
            </el-select>
          </el-form-item></el-col>
        <!-- <el-col :span="8">
          <el-form-item label="类型" prop="type">
            <el-select v-model="dataForm.type" placeholder="类型">
              <el-option label="种猪" value="种猪"></el-option>
              <el-option label="生长猪" value="生长猪"></el-option>
            </el-select>
          </el-form-item></el-col> -->
        <el-col :span="12">
          <el-form-item label="死淘日期" prop="deadDate">
            <el-date-picker
                v-model="dataForm.deadDate"
                align="right"
                type="date"
                placeholder="死淘日期"
                :picker-options="pickerOptions"
                value-format="yyyy-MM-dd">
            </el-date-picker>
          </el-form-item></el-col>
        <el-col :span="12">
          <el-form-item size='small' label="栏位" prop="preGrid">
            <el-select size='mini' style="width: 90px" v-model="dataForm.grid.row" placeholder="行">
              <el-option
                v-for="item in rows"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
            <el-select size='mini' style="width: 90px" v-model="dataForm.grid.column" placeholder="列">
              <el-option
                v-for="item in columns"
                :key="item.value"
                :label="item.label"
                :value="item.value">
              </el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="耳号" prop="earmark">
            <Earmark placeholder="耳号" v-model="dataForm.sow"></Earmark>
          </el-form-item></el-col>
        <!-- <el-col :span="8">
          <el-form-item label="头数" prop="mounts">
            <el-input v-model="dataForm.mounts" placeholder="头数" type="number"></el-input>
          </el-form-item></el-col> -->
        <el-col :span="12">
          <el-form-item label="日龄" prop="age">
            <el-input v-model="dataForm.age" placeholder="日龄" type="number"><i slot="suffix" style="font-style:normal;margin-right: 10px;">天</i></el-input>
          </el-form-item></el-col>
        <el-col :span="12">
          <el-form-item label="重量" prop="weight">
            <el-input v-model="dataForm.weight" placeholder="重量" type="number"><i slot="suffix" style="font-style:normal;margin-right: 10px;">kg</i></el-input>
          </el-form-item></el-col>
        <el-col :span="12">
          <el-form-item label="操作人" prop="operator">
            <UserSelect v-model="dataForm.operator"></UserSelect>
          </el-form-item></el-col>
        <el-col :span="12">
          <el-form-item label="原因" prop="reason">
            <RenSelect dictType="mortality_reason" placeholder="原因" v-model="dataForm.reason"></RenSelect>
            <!-- <el-input v-model="dataForm.reason" placeholder="原因 病毒性腹泻 大肠杆菌 圆环 猪瘟 流感 其它"></el-input> -->
          </el-form-item></el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="remark">
            <el-input v-model="dataForm.remark" placeholder="备注"></el-input>
          </el-form-item></el-col>
      </el-row>
<!--      <el-form-item label="更新者" prop="updater">-->
<!--        <el-input v-model="dataForm.updater" placeholder="更新者"></el-input>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="更新时间" prop="updateDate">-->
<!--        <el-input v-model="dataForm.updateDate" placeholder="更新时间"></el-input>-->
<!--      </el-form-item>-->
<!--      <el-form-item label="" prop="tenantId">-->
<!--        <el-input v-model="dataForm.tenantId" placeholder=""></el-input>-->
<!--      </el-form-item>-->
    </el-form>
    <Grid v-if="dataForm.sow.id" :pig="dataForm.sow" v-on:breedSelect="breedSelect" :house_id="dataForm.sow.houseId"></Grid>
    <template slot="footer">
      <el-button @click="visible = false">{{ $t('cancel') }}</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{ $t('confirm') }}</el-button>
    </template>
  </el-dialog>
</template>

<script>/* eslint-disable */
import debounce from 'lodash/debounce'

export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: '',
        houseId: '',
        houseName: '',
        type: '种猪',
        category: '死亡',
        deadDate: '',
        grid: '',
        earmark: '',
        mounts: 1,
        age: '',
        weight: '',
        operator: '',
        reason: '',
        remark: '',
        creator: '',
        createDate: '',
        updater: '',
        updateDate: ''
      },
      houses: [],
      rows: [],
      columns: []
    }
  },
  computed: {
    dataRule() {
      return {
        houseId: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        houseName: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        category: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        type: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        deadDate: [
          {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        ],
        // grid: [
        //   {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        // ],
        // earmark: [
        //   {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        // ],
        // mounts: [
        //   {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        // ],
        // age: [
        //   {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        // ],
        // weight: [
        //   {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        // ],
        // operator: [
        //   {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        // ],
        // reason: [
        //   {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        // ],
        // remark: [
        //   {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        // ],
        // updater: [
        //   {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        // ],
        // updateDate: [
        //   {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        // ],
        // tenantId: [
        //   {required: true, message: this.$t('validate.required'), trigger: 'blur'}
        // ]
      }
    }
  },
  created() {
    this.getHouses()
  },
  methods: {
    breedSelect(pig) {
      this.dataForm.sow = pig
    },
    init() {
      this.visible = true
      this.$nextTick(() => {
        this.$refs['dataForm'].resetFields()
        if (this.dataForm.id) {
          this.getInfo()
        }
      })
    },
    house_change() {
      let house = this.houses[this.dataForm.toHouseId]
      this.rows = []
      this.columns = []
      for (let i=0; i<house.rows; ++i) {
        this.rows.push({
          value: i+1,
          label: i+1+'行'
        })
      }
      for (let i=0; i<house.columns; ++i) {
        this.columns.push({
          value: i+1,
          label: i+1+'列'
        })
      }
    },
    getHouses () {
      let that = this;
      this.$http.get(`/basic/house/all`).then(({ data: res }) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        res.data.forEach(function(house){
          that.houses[house.id] = house;
        });
        this.house_change()
        that.$forceUpdate()
      }).catch(() => {})   
    },
    // 获取信息
    getInfo() {
      this.$http.get(`/history/deadmortality/${this.dataForm.id}`).then(({data: res}) => {
        if (res.code !== 0) {
          return this.$message.error(res.msg)
        }
        this.dataForm = {
          ...this.dataForm,
          ...res.data
        }
      }).catch(() => {
      })
    },
    // 表单提交
    dataFormSubmitHandle: debounce(function () {
      if (this.dataForm.sow) {
        this.dataForm.pigId = this.dataForm.sow.id;
        this.dataForm.earmark = this.dataForm.sow.earmark;
      }
      this.$refs['dataForm'].validate((valid) => {
        if (!valid) {
          return false
        }
        this.$http[!this.dataForm.id ? 'post' : 'put']('/history/deadmortality/', this.dataForm).then(({data: res}) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg)
          }
          this.$message({
            message: this.$t('prompt.success'),
            type: 'success',
            duration: 500,
            onClose: () => {
              this.visible = false
              this.$emit('refreshDataList')
            }
          })
        }).catch(() => {
        })
      })
    }, 1000, {'leading': true, 'trailing': false})
  }
}
</script>
